package Entity;

import java.util.Vector;
import java.util.*;

public abstract class Crosstab {

    public static Vector<Rank> getRankings(int[][] coverageMatrix, int[] resultVector) //Don't really need other 2...they can be computed
    {
        Vector<Rank> rankings = new Vector<Rank>();
        rankings.clear();

        double numTests = coverageMatrix.length;
        int numStmts = coverageMatrix[0].length;

        double numPassed = 0;
        double numFailed = 0;

        //System.out.println(numTests+" "+numStmts);

        for (int i = 0; i < resultVector.length; i++) {
            if (resultVector[i] > 0) {
                numFailed++;
            } else {
                numPassed++;
            }
        }

        if (((numFailed + numPassed) > numTests) || (numFailed + numPassed) < numTests) {
            System.out.println("WTF");
        }

        for (int i = 0; i < numStmts; i++) {
            double failedAndExecuted = 0;
            double passedAndExecuted = 0;
            double failedDidNotExecute = 0;
            double passedDidNotExecute = 0;

            for (int j = 0; j < numTests; j++) {
                if (coverageMatrix[j][i] > 0) {
                    if (resultVector[j] == 1) {
                        failedAndExecuted++;
                    } else {
                        passedAndExecuted++;
                    }
                } else {
                    if (resultVector[j] == 1) {
                        failedDidNotExecute++;
                    } else {
                        passedDidNotExecute++;
                    }
                }
            }


            double e1 = ((failedAndExecuted + passedAndExecuted) * numFailed) / numTests;
            double e2 = ((failedAndExecuted + passedAndExecuted) * numPassed) / numTests;
            double e3 = ((failedDidNotExecute + passedDidNotExecute) * numFailed) / numTests;
            double e4 = ((failedDidNotExecute + passedDidNotExecute) * numPassed) / numTests;

            double chi_squared = (((failedAndExecuted - e1) * (failedAndExecuted - e1)) / e1)
                    + (((passedAndExecuted - e2) * (passedAndExecuted - e2)) / e2)
                    + (((failedDidNotExecute - e3) * (failedDidNotExecute - e3)) / e3)
                    + (((passedDidNotExecute - e4) * (passedDidNotExecute - e4)) / e4);

            double contingency_coefficient = chi_squared / numTests;

            double sign_decider = (failedAndExecuted / numFailed) / (passedAndExecuted / numPassed);

            double susp;

            if (sign_decider > 1) {
                susp = contingency_coefficient;
            } else if (sign_decider < 1) {
                susp = (contingency_coefficient * (-1));
            } else {
                susp = 0;
            }

            Rank r = new Rank(susp, i);
            rankings.add(r);
        }

        Collections.sort(rankings);

        /*
        for(int i=0;i<rankings.size();i++)
        {
        System.out.println(rankings.elementAt(i).stmt+"\t"+rankings.elementAt(i).susp+"\t"+(i+1));
        }
        
        System.out.println("----- END CROSSTAB-----");
         */

        return ((Vector<Rank>) (rankings.clone()));
    }
}